<?php

namespace App\Http\Validate\Cruise;

use App\Http\Validate\BaseValidate;
use App\Models\Cruise\CruiseTaskProjectModel;
use App\Support\CustomForm\AttributeCruiseSupport;
use App\Support\CustomForm\AttributeSupport;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

class CruiseProjectValidate extends BaseValidate
{
    public static function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    public static function getRules(array $fields = []): array
    {
        return self::getActuallyRules([
            'categoryId'       => 'required|integer',
            'name'             => 'required|string',
            'tipsType'         => ['string', Rule::in(CruiseTaskProjectModel::TIPS_TYPES)],
            'tipsContent'      => 'string',
            "tipsResource"     => 'array',
            'status'           => 'required|integer|between:0,1',
            'inputType'        => ['required', 'string', Rule::in(CruiseTaskProjectModel::INPUT_TYPES)],
            'maxContentLength' => 'integer|min:0',
            'options'          => 'required_if:inputType,' . AttributeSupport::INPUT_TYPE_SELECT . '|array',
            'options.*.text'   => 'required_if:inputType,' . AttributeSupport::INPUT_TYPE_SELECT,
            'options.*.value'  => 'required_if:inputType,' . AttributeSupport::INPUT_TYPE_SELECT,
            'options.*.score'  => ['integer', Rule::requiredIf(function () use ($fields) {
                return in_array($fields['inputType']  ?? '', AttributeCruiseSupport::CRUISE_CHECK_MODULE);
            })],
            'options.*.remark' => 'string',
            'required'         => 'required|bool',
            'richShow'         => 'string',
        ]);
    }

    /**
     * 更新校验
     *
     * @param array $requestParams
     * @return array
     * @throws ValidationException
     */
    public static function addValidate(array $requestParams): array
    {
        $validateMsg = [
            'options.*.score.required_if' => '巡检项的分数不能为空'
        ];

        return self::baseValidate($requestParams, self::getRules($requestParams), null, null, $validateMsg);
    }
}
